package com.hccl.SMSDetector;

import java.io.*;
import java.util.Properties;

/**
 * Created by Zhao on 2016/03/25.
 */
public class SMSFileReader implements SMSReader {
    public static String encode = "utf8";

    private String filepath;
    private InputStreamReader inputStreamReader;
    private BufferedReader bufferReader;
    static {
        Properties property = new Properties();
        try {
            property.load(new FileInputStream("config.inf"));
            encode = property.getProperty("encode","utf8");
            System.out.println("读取文本编码格式: "+encode);
        } catch (Exception e) {
            System.err.println("Loading configuration failed, please check. Using default parameters.");
            System.exit(-1);
        }
    }

    public SMSFileReader(String filepath) {
        this.filepath = filepath;
    }
    public SMSFileReader() {

    }
    public boolean init() {
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(filepath), "utf8");
            bufferReader = new BufferedReader(inputStreamReader);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public SMSEntity getNextSMS() {
        String strLine;
        String content="";
        try {
            while(bufferReader.ready()&&(strLine=bufferReader.readLine().trim())!= null){
                strLine = strLine.trim().replaceAll("[^\\u4E00-\\u9FA5]","");
                content += strLine;
            }
            SMSEntity sms = new SMSEntity();
            sms.setSmsContent(content);
            if (content.length() < 5) sms.setExclude(true);
            return sms;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public boolean init(File file) {
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file), encode);
            bufferReader = new BufferedReader(inputStreamReader);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //读取一个文件夹，一个文件生成一个SMSEntity
    public SMSEntity getNextSMS(File file) {
        String strLine;
        String content="";
        try {
            while(bufferReader.ready()&&(strLine=bufferReader.readLine().trim())!= null){
                strLine = strLine.trim().replaceAll("[^\\u4E00-\\u9FA5]","");
                content += strLine;
            }
            SMSEntity sms = new SMSEntity();
            sms.setSmsContent(content);
            sms.setFilename(file.getName());
            if (content.length() < 5) sms.setExclude(true);
            return sms;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //读取一个字符串生成一个SMSEntity
    public SMSEntity getNextSMS(String name,String content) {
        if(content.equals("")||content==null||name.equals("")||name==null)return null;
        SMSEntity sms = new SMSEntity();
        sms.setSmsContent(content);
        sms.setFilename(name);
        if (content.length() < 5) sms.setExclude(true);
        return sms;
    }


    public void close() {
        try {
            bufferReader.close();
            inputStreamReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
